﻿using UnityEngine;

public class ParticleCase
{
    private ParticleSystem particleSystem;
    public ParticleSystem ParticleSystem => particleSystem;

    private float disableTime = -1;

    private bool forceDisable;

    public ParticleCase(Particle particle)
    {
        // Create object
        GameObject particleObject = particle.ParticlePool.GetPooledObject();
        particleObject.SetActive(true);

        // Get particle component
        particleSystem = particleObject.GetComponent<ParticleSystem>();
        particleSystem.Play();
    }

    public ParticleCase SetPosition(Vector3 position)
    {
        particleSystem.transform.position = position;

        return this;
    }

    public ParticleCase SetScale(Vector3 scale)
    {
        particleSystem.transform.localScale = scale;

        return this;
    }

    public ParticleCase SetRotation(Quaternion rotation)
    {
        particleSystem.transform.localRotation = rotation;

        return this;
    }

    public ParticleCase SetDuration(float duration)
    {
        disableTime = Time.time + duration;

        return this;
    }

    public ParticleCase SetTarget(Transform followTarget, Vector3 localPosition)
    {
        particleSystem.transform.SetParent(followTarget);
        particleSystem.transform.localPosition = localPosition;

        return this;
    }

    public void OnDisable()
    {
        particleSystem.transform.SetParent(null);
        particleSystem.Stop();

        particleSystem.gameObject.SetActive(false);
    }

    public void ForceDisable()
    {
        forceDisable = true;
    }

    public bool IsForceDisabledRequired()
    {
        if (forceDisable)
            return true;

        if (disableTime != -1 && Time.time > disableTime)
            return true;

        return false;
    }
}
